package hot100;

public class HasCycle {
    public class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
            next = null;
        }
    }

    public boolean hasCycle(ListNode head) {
        if (head == null) return false;

        if (head.next == null) return false;

        if (head.next == head) return true;
        //快慢指针
        ListNode slow = head,fast = head.next;
        while (fast != null && fast.next != null && fast != slow) {
            fast = fast.next.next;
            slow = slow.next;
        }

        if (fast == null || fast.next == null) return false;
        return true;
    }
}
